# ��/bin/python
import socket

# ���ֵ���ʽ��ͳ�Ƹ���������������
countdict = {"error": 0}
# ��������
request = '''HEAD / HTTP/1.1
Accept: */*
Host: %(sip)s
Connection: Keep-Alive

'''
# print(countdict)
print(
"{0:*^6}".format("HOST"), "{0:*^26}".format("WEB SITE"), "{0:*^6}".format("SERVER:"), "{0:*^26}".format("Server Name"))
print("-----------------------------------------------------------------")
fp = open('serverlist.txt', 'r')
for serverip in fp:
    serverip = serverip.strip()
    # print(request % dict(sip = serverip) )
    # ��Բ�ͬ��վ�滻�������Host�ֶ�
    tt = request % dict(sip=serverip)
    # print(tt)
    # ����header
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((serverip, 80))
        s.send(tt.encode())
        recv = s.recv(1024)
        recv = recv.decode()
        # print(recv.decode())
    except TimeoutError as te:
        print("HOST: ", serverip, "    �����ӳ�ʱ����")
        countdict["error"] = + 1
        print("-----------------------------------------------------------------")
        continue
    except Exception as e:
        print("HOST: ", serverip, "    ��δ֪����")
        countdict["error"] = + 1
        print("-----------------------------------------------------------------")
        continue
    print(recv)
    recvlist = recv.split('\r\n')
    for a in recvlist:
        # if 'Server:' in a and a.startswith('Server:'):
        if a.startswith('Server:'):
            servername = a.split(':')[1]

    # ͳ��Ӧ�õķ���������
    if servername in countdict:
        countdict[servername] += 1
    else:
        countdict[servername] = 1

    # ��ʽ����ӡ
    # print("HOST: ", serverip, "Server: ", servername)
    print(
    "{0: <6}".format("HOST: "), "{0: <26}".format(serverip), "{0: <6}".format("Server:"), "{0: <26}".format(servername))
    print("-----------------------------------------------------------------")

# ��ӡͳ�ƽ��
print(countdict)
